package com.dingwei.superiordiary.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.allen.library.SuperTextView;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.bean.SafeSetBean;
import com.dingwei.superiordiary.mvp.presenter.SafeSetPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.DialogUtil;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.L;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.Base;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 南柯一梦丶
 *         Created time 2018/10/25.
 *         Personal signature 余生还长 何必慌张
 *         Page description :
 *         .::::.
 *         .::::::::.
 *         :::::::::::
 *         ..:::::::::::'
 *         '::::::::::::'
 *         .::::::::::
 *         '::::::::::::::..
 *         ..::::::::::::.
 *         ``::::::::::::::::
 *         ::::``:::::::::'        .:::.
 *         ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *         .:::'       :::::  .:::::::::' ':::::.
 *         .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *         ...:::           ::::::::::::'              ``::.
 *         ```` ':.          ':::::::::'                  ::::..
 *         '.:::::'                    ':'````..
 */
public class SafeSetActivity extends BaseActivity implements Contract.SafeSetView {

    SafeSetPresenter safeSetPresenter = new SafeSetPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.email)
    SuperTextView email;
    @Bind(R.id.phone)
    SuperTextView phone;
    @Bind(R.id.weixin)
    SuperTextView weixin;
    @Bind(R.id.QQ)
    SuperTextView QQ;
    @Bind(R.id.modify_password)
    SuperTextView modifyPassword;
    private UMShareAPI mShareAPI;

    @Override
    protected int getContentViewId() {
        return R.layout.activty_safe_set;
    }

    @Override
    protected void initData() {
        safeSetPresenter.initData();
    }

    @Override
    protected void initView() {

        tvTitle.setText("安全设置");
        mShareAPI = UMShareAPI.get(context);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(SafeSetActivity.this).setShareConfig(config);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.weixin, R.id.QQ, R.id.modify_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.weixin:
                if (weixin.getRightString().equals("未绑定")) {
                    mShareAPI.getPlatformInfo(SafeSetActivity.this, SHARE_MEDIA.WEIXIN, umAuthListener);
                } else {
                    DialogUtil.showAlertDialogChoose(BaseActivity.context, "温馨提示", "是否解绑微信账号", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogUtil.BUTTON1:
                                    dialog.dismiss();
                                    break;
                                case DialogUtil.BUTTON2://确认
                                    dialog.dismiss();
                                    safeSetPresenter.WXlogin("WEIXIN", "");
                                    break;
                            }
                        }
                    });
                }
                break;
            case R.id.QQ:
                if (QQ.getRightString().equals("未绑定")) {
                    mShareAPI.getPlatformInfo(SafeSetActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                } else {
                    DialogUtil.showAlertDialogChoose(BaseActivity.context, "温馨提示", "是否解绑QQ账号", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogUtil.BUTTON1:
                                    dialog.dismiss();
                                    break;
                                case DialogUtil.BUTTON2://确认
                                    dialog.dismiss();
                                    safeSetPresenter.WXlogin("QQ", "");
                                    break;
                            }
                        }
                    });
                }
                break;
            case R.id.modify_password:
                intent = new Intent(BaseActivity.context,ForGetPasswordActivity.class);
                break;
        }
    }

    @Override
    public void onFail() {

    }

    @Override
    public void setData(SafeSetBean bean) {
        phone.setRightString(bean.data.bind_mobile);
        weixin.setRightString(bean.data.bind_wx);
        QQ.setRightString(bean.data.bind_qq);
    }

    @Override
    public void unbindQQSuccess() {
        safeSetPresenter.initData();
    }

    @Override
    public void bindQQSuccess() {
        safeSetPresenter.initData();
    }

    @Override
    public void unbindWXSuccess() {
        safeSetPresenter.initData();
    }

    @Override
    public void bindWXSuccess() {
        safeSetPresenter.initData();
    }


    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
            dialog.show();
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            L.e(platform.toString());
            safeSetPresenter.WXlogin(platform.toString(), data.get("uid"));
            dialog.dismiss();

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getApplicationContext(), "授权失败", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getApplicationContext(), "取消授权", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
